#include "commt.h"

int main()
{
    key_t key = ftok(FILENAME, PROJ_ID);
    if(key < 0)
    {
        printf("ftok fail!\n");
        exit(1);
    }

    int shmid = shmget(key, SIZE, IPC_CREAT | IPC_EXCL | 0666);
    if(shmid < 0)
    {
        printf("shmget fail!\n");
        exit(2);
    }

    std::cout << "key: " << key << std::endl;
    std::cout << "shmid: " << shmid << std::endl;

    //挂接
    printf("shmat begin!\n");
    char *mem = (char *)shmat(shmid, NULL, 0);
    if(mem == (void*)-1)
    {
        printf("shmat fail!\n");
        exit(3);
    }
    printf("shmat success!\n");
    while(1)
    {
        printf("Client Sendto: %s\n", mem);
        sleep(1);
    }

    //去关联
    shmdt(mem);

    //删除共享内存
    shmctl(shmid, IPC_RMID, NULL);

    return 0;
}